Sounds like a good place for an if-else statement.
Here is the not-quite finished program:
import java.io.*; // User enters a value N // Add up odd integers, even integers, and all integers 0 to N // class addUpIntegers { public static void main (String[] args ) throws IOException { BufferedReader userin = new BufferedReader (new InputStreamReader(System.in)); String inputData; int N, sumAll = 0, sumEven = 0, sumOdd = 0; System.out.println( "Enter limit value:" ); inputData = userin.readLine(); N = Integer.parseInt( inputData ); int count = 0 ; while ( count <= N ) { sumAll = ______________ ; if ( ______________ ) sumEven = ______________ ; else sumOdd = ______________ ; count = count + 1 ; } System.out.print ( "Sum of all : " + sumAll ); System.out.print ( "\tSum of even: " + sumEven ); System.out.println( "\tSum of odd : " + sumOdd ); } }
The loop body in this program contains an if
statement.
This is fine.
An if
statement inside of a loop body is called a nested if.
There is nothing special about it; it works just as it would
outside of the loop body.